<div class="relationAttributes">
  <% relation_attributes = relation.relationAttributes
     sticky_map          = relation.respond_to?(:relationAttrProps) && relation.relationAttrProps.respond_to?(:owner)
     md_attrs            = relation.meta.attributes[:mdAttributes]%>
  <%= f.fields_for :relationAttributes, relation_attributes do |c| %>
    <% if defined?(only)
         only_map = only.inject({}) {|m, name| m[name.to_s] = name; m}
         md_attrs = md_attrs.select {|a| only_map.include?(a.attributeName)}
       end %>
    <% md_attrs.group_by { |a| a.options.has_key?(:category) ? a.options[:category] : 'Attributes' }.sort.each do |category, attribute_list| %>
      <div class="category">
        <div class="name"><%= category.split('.').last %></div>
        <% attribute_list.sort_by { |a| a.options.has_key?(:order) ? a.options[:order] : 999 }.each do |a| %>
          <% pattern         = a.options[:pattern]
             required        = a.isMandatory ? true : nil
             attribute_value = relation_attributes.send(a.attributeName)
             placeholder     = a.isMandatory && attribute_value.blank? ? 'this field is required' : nil
             field_type      = a.options.has_key?(:form) ? a.options[:form][:field] : ''
             editable        = (!(relation.respond_to?(:ciRelationId) && relation.ciRelationId.to_i > 0 && a.options.has_key?(:editable) && !a.options[:editable]))%>
          <div class="control-group" editable="<%= editable %>">
            <%= c.label a.attributeName,
                        link_to("#{a.description}#{'*' if a.isMandatory}", '#', :tabindex => '-1', :rel => 'tooltip', :title => "#{a.options[:help].presence || a.description}"),
                        :class => "control-label #{'sticky' if sticky_map} #{'required' if required}" %>
            <% if sticky_map %>
              <div class="input-prepend lock-container">
                <%= render 'cms/cis/sticky',
                           :name  => "#{f.object_name}[relationAttrProps][owner][#{a.attributeName}]",
                           :value => relation.relationAttrProps.owner.attributes[a.attributeName] %>
              </div>
            <% end %>
            <div class="controls" editable="<%= editable %>">
              <% case field_type %>
              <% when 'textarea' %>
                <%= c.text_area a.attributeName, :class => 'input-xlarge text', :required => required, :placeholder => placeholder, :pattern => pattern %>
              <% when 'checkbox' %>
                <% if a.options[:form][:multiple] == 'true' %>
                  <%= render 'cms/cis/multiselect',
                             :attribute_name     => a.attributeName,
                             :attribute_value    => attribute_value,
                             :options_for_select => a.options[:form][:options_for_select],
                             :builder            => c %>
                <% else %>
                  <%= render 'cms/cis/checkbox',
                             :attribute_name  => a.attributeName,
                             :attribute_value => attribute_value,
                             :builder         => c %>
                <% end %>
              <% when 'select' %>
                <%= c.select a.attributeName, options_for_select(a.options[:form][:options_for_select], attribute_value) %>
              <% else %>
                <%= c.text_field a.attributeName, :class => 'input-xlarge', :required => required, :placeholder => placeholder, :pattern => pattern %>
              <% end %>
            </div>
          </div>
        <% end %>
      </div>
    <% end %>
  <% end %>
</div>
